package com.java.service.impl;

import com.java.entity.PageBean;
import com.java.entity.PsRole;
import com.java.mapper.PsRoleMapper;
import com.java.service.PsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author caiyuan
 * @version 1.0
 * @Date 2020/6/18 10:23 Dream-Seeker
 * @Description
 */
@Service
public class PsRoleServiceImpl implements PsRoleService {

    @Autowired
    PsRoleMapper psRoleMapper;

    @Override
    public PageBean<PsRole> findByPage(int currentPage) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        PageBean<PsRole> pageBean = new PageBean<PsRole>();

        //每页显示的数据
        int pageSize = 5;
        pageBean.setPageSize(pageSize);

        //封装总记录数
        int totalCount = psRoleMapper.selectCount();
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);//向上取整
        pageBean.setTotalPage(num.intValue());

        if (currentPage > pageBean.getTotalPage()) {
            //封装当前页数
            pageBean.setCurrPage(pageBean.getTotalPage());
        } else if (currentPage < 0) {
            pageBean.setCurrPage(1);
        } else {
            pageBean.setCurrPage(currentPage);
        }

        map.put("start", (currentPage - 1) * pageSize);
        map.put("size", pageBean.getPageSize());
        //封装每页显示的数据
        List<PsRole> lists = psRoleMapper.findByPage(map);
        pageBean.setLists(lists);

        return pageBean;
    }
}
